package gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import connector.Connector;



public class MapPanel extends JPanel 
{
    private Connector mapDelegate = null;
    private boolean shown = false;
    
    public MapPanel() 
    {   /*       
        this.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
        DebugGraphics.setFlashTime(750);
        DebugGraphics.setFlashCount(3);
        RepaintManager.currentManager(null).setDoubleBufferingEnabled(false);
        */
        this.setOpaque(true);
        this.setFocusable(true);
        
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public Dimension getPreferredSize() 
    {
        return new Dimension(800,600);
    }
    
    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);                
                
        if(mapDelegate!=null)
        {
            Image mapImg = mapDelegate.getMapImage();
            if(mapImg!=null)
                g.drawImage(mapImg, 0,0,null);
        }
        
        
    }

    public void setMapDelegate(Connector mapDelegate) {
        this.mapDelegate = mapDelegate;
    }

    public Connector getMapDelegate() {
        return mapDelegate;
    }
    
    private void _componentHidden(ComponentEvent e) {
        shown = false;
    }

    private void _componentResized(ComponentEvent e) {
        mapDelegate.setViewportSize(getSize());  
        this.repaint();
    }

    private void _componentShown(ComponentEvent e) {
        shown = true;
        System.out.println("Map panel displayed.");
    }

    private void _keyPressed(KeyEvent e) {
        boolean validKey = true;
        //System.out.println("key pressed: "+e.getKeyCode());
        switch(e.getKeyCode()) {
        case KeyEvent.VK_DOWN: 
            mapDelegate.panSouth();
            break;
        case KeyEvent.VK_UP:
            mapDelegate.panNorth();
            break;
        case KeyEvent.VK_LEFT:
            mapDelegate.panWest();
            break;
        case KeyEvent.VK_RIGHT:
            mapDelegate.panEast();
            break;
        case KeyEvent.VK_ADD:
            mapDelegate.zoomIn();
            break;
        case KeyEvent.VK_SUBTRACT:
            mapDelegate.zoomOut();
            break;
        default:
            validKey = false;
        }
        
        if(validKey)
            repaint();
    }

    private void _mouseClicked(MouseEvent e) {
        if( e.getClickCount() >= 2) {
            int x = (int) e.getPoint().getX();
            int y = (int) e.getPoint().getY();
            mapDelegate.recenter(x,y);
            repaint();
        }
    }
    
    private void jbInit() throws Exception {
        this.addComponentListener(new ComponentAdapter() {
                    public void componentResized(ComponentEvent e) {
                        _componentResized(e);
                    }

                    public void componentHidden(ComponentEvent e) {
                        _componentHidden(e);
                    }

                    public void componentShown(ComponentEvent e) {
                        _componentShown(e);
                    }
                });
        
        this.addKeyListener(new KeyAdapter() {
                    public void keyPressed(KeyEvent e) {
                        _keyPressed(e);
                    }
                });
        this.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent e) {
                        _mouseClicked(e);
                    }
                });
    }


}